
var chessMaster = function(){

	var tForm   = new transform();
	var migDir  = 'images/';
	var migExt  = '.png';

	var popDur  = 800;//millisecond duration for pop animation
	var movDur  = 500;//millisecond duration for moving pieces
	var wigDur  = 500;//millisecond duration for moving pieces
	var setPaw  = 100;//milliseconds  between  piece placement

	this.changePopDuration    = function(i){popDur = i;};
	this.changeMoveDuration   = function(i){movDur = i;};
	this.changeWiggleDuration = function(i){wigDur = i;};
	this.changeSetupPause     = function(i){setPaw = i;};

	var curLang = 0;
	this.changeLang = function(i){ if(msgs[i]) curLang = i;};
	var msgs    = [
		['no such id','board width not a multiple of 8'],
		['no hay el id','ancha de planca no es divisible por 8'],
		['Es gibt nicht so ID','die Breite des Boards ist nicht durch 8 teilbar']
	];

	var boardArea; 
	this.isNotAnimating = true;
	var spaceWidth;
	var spaceHeight;
	var spaceWidth2;
	var spaceHeight2;
	var coords;// = new Array();
	var pieces;// = new Array();

	this.init = function(boardArea,migList){

		pieces = new Array();

	var boardArea = (typeof(boardArea)=='string')?document.getElementById(boardArea) : (!boardArea)? alert(msgs[curLang][0]) : boardArea;

	//  if the boardArea is NOT a multiple of 8 pixels 
	//  that's going to be a problem, but rounding anyway

	if(boardArea.offsetWidth%8 != 0 || boardArea.offsetHeight%8 != 0){
		//  chrome subtracts one from the offsetWidth
		//  don't know why and not going to try to
		//  compensate - will just eliminate this alert
		//alert(msgs[curLang][1]);
	}

	//  remove any kids from the boardArea

	if(boardArea.hasChildNodes()){
		while(boardArea.childNodes.length >= 1){
			boardArea.removeChild(boardArea.firstChild);
		}
	}

	//  calculate and store pixel position & dimensions of 
	//  the _CENTER_ of each square

	spaceWidth   = Math.round(boardArea.offsetWidth/8 );
	spaceHeight  = Math.round(boardArea.offsetHeight/8);
	spaceWidth2  = Math.round(spaceWidth/2 );
	spaceHeight2 = Math.round(spaceHeight/2);
	coords = new Array();
	var tmpAlpha = 'abcdefgh'.split('');


	for (var i=0;i<8;i++){
		for(var j=8;j>0;j--){
			var tmpName = tmpAlpha[i]+(9-j);
			coords[tmpName] = [spaceHeight2+spaceHeight*(j-1),spaceWidth2+spaceWidth*i];
		}
	}	

	var popImage = function(mig,dur){

		var migS = mig.style;

		var now = +new Date;

		var diff = (now - mig.oldTime)/dur;

		if(diff > 1 ){
			migS.height = mig.hhh +'px';
			migS.width  = mig.www +'px';
			migS.top  = mig.old[0] - mig.hh2 +'px';
			migS.left = mig.old[1] - mig.ww2 +'px';
			mig.popComplete.call();
		}else{

		var per  = tForm.popOut(diff);

		var tmpH = per*mig.hhh;
		var tmpW = per*mig.www;
			

			migS.height = tmpH +'px';
			migS.width  = tmpW +'px';

		migS.top  = Math.round(mig.old[0] - tmpH/2) +'px';
		migS.left = Math.round(mig.old[1] - tmpW/2) +'px';

			setTimeout(function(){popImage(mig,dur)},0);
		}

	};

	var pinImage = function(mig,dur){

		var migS = mig.style;

		var now = +new Date;

		var diff = (now - mig.oldTime)/dur;


		if(diff > 1){
			migS.visibility = 'hidden';
			migS.height = mig.hhh +'px';
			migS.width  = mig.www +'px';
			migS.top  = mig.old[0] - mig.hh2 +'px';
			migS.left = mig.old[1] - mig.ww2 +'px';
			CM.isNotAnimating = true;
			mig.pinComplete.call();
		}else{

		var per = tForm.popIn(diff);

		var tmpH = Math.round(per*mig.hhh);
		var tmpW = Math.round(per*mig.www);
		
			
			migS.height = tmpH +'px';
			migS.width  = tmpW +'px';

		migS.top  = Math.round(mig.old[0] - tmpH/2) +'px';
		migS.left = Math.round(mig.old[1] - tmpW/2) +'px';

			setTimeout(function(){pinImage(mig,dur)},0);
		}

	};


	//  add all piece images to the boardArea

	for(var i=0;i<migList.length;i++){

		var tmpFile = migList[i][0];
		var tmpID   = migList[i][1];

		pieces[tmpID] = new Image();
		pieces[tmpID].src = migDir+tmpFile+migExt;
		pieces[tmpID].old = [0,0];
		pieces[tmpID].id  = tmpID;
		pieces[tmpID].pop = function(){popImage(this,popDur);};
		pieces[tmpID].oldTime = '';//+new Date;
		pieces[tmpID].pin = function(){pinImage(this,popDur);};
		pieces[tmpID].pow = function(){wiggleImage(this,wigDur);};
		pieces[tmpID].onclick = function(){
			if(CM.isNotAnimating){
				CM.isNotAnimating = false;
				this.oldTime = +new Date;
				wiggleImage(this,wigDur);
			}
		};
		pieces[tmpID].popComplete = function(){};
		pieces[tmpID].pinComplete = function(){};
		pieces[tmpID].onload = function(){
			//height/width values need to be gotten after load
			this.hhh = this.height;
			this.www = this.width;
			this.hh2 = Math.round(this.hhh/2);
			this.ww2 = Math.round(this.www/2);
			//then erase this
			this.onload = function(){}; 
		}

	
		var tmpS = pieces[tmpID].style;

		tmpS.position = 'absolute';
		tmpS.top  = '0px';
		tmpS.left = '0px';
		tmpS.visibility = 'hidden';

		boardArea.appendChild(pieces[tmpID]);


	}
	//this.initComplete();

	};

	var wiggleImage = function(migID,dur){

		if(fSteps[curStep] && fSteps[curStep].split(':')[0] == migID.id){
			//alert('right piece');
			CM.isNotAnimating = true;
			CM.nextStep();
		}else{

		var now = +new Date;

		var diff = (now - migID.oldTime)/dur;

		if(diff < 1 ){

		var per = tForm.wiggle(diff,1);

		var tmpW = per*migID.www;
		var tmpH = per*migID.hhh;

		migID.style.top  = Math.round(migID.old[0]-tmpW/2) +'px';
		migID.style.left = Math.round(migID.old[1]-tmpH/2) +'px';
		migID.style.width  = Math.round(tmpW) + 'px';
		migID.style.height = Math.round(tmpH) + 'px';

		var tmpF = function(){wiggleImage(migID,dur);};

		setTimeout(tmpF,0);


		}else{

		migID.style.top  = Math.round(migID.old[0]-migID.hh2) +'px';
		migID.style.left = Math.round(migID.old[1]-migID.ww2) +'px';
		migID.style.width  = migID.www + 'px';
		migID.style.height = migID.hhh + 'px';

		CM.isNotAnimating = true;

		}
		
	}};	

	var powImage = function(){
	};

	var startPos = new Array();
	var fSteps   = new Array();
	var bSteps   = new Array();
	var curStep  = 0;

	var setupComplete = function(){};

	this.onSetupComplete = function(func){
		setupComplete = func;
	};

	var setupXXX = function(i){

		var x, ps;

		if(i<startPos.length){
			x  = startPos[i].split(':');
			if(x[1].indexOf(';') != -1){
				var tmpX = x[1].split(';');
				x[1] = tmpX[0];
				window.notes.innerHTML += tmpX[1];
			}
			ps = pieces[x[0]].style;
			ps.top  = coords[x[1]][0] - pieces[x[0]].hh2 +'px';
			ps.left = coords[x[1]][1] - pieces[x[0]].ww2 +'px';
			ps.visibility = 'visible';
			pieces[x[0]].old = coords[x[1]];
			pieces[x[0]].oldTime = +new Date;
			pieces[x[0]].pop();
		}
		if(i == startPos.length-1){
			x = startPos[i].split(':');
			if(x[1].indexOf(';') != -1){
				var tmpX = x[1].split(';');
				x[1] = tmpX[0];
			}
			pieces[x[0]].popComplete = function(){
				setupComplete();
				setupComplete = function(){};
				CM.isNotAnimating = true;
			}
		}else{
			var hack = i+1;
		 	var tmpF = function(){setupXXX(hack);};
			setTimeout(tmpF,setPaw);
		}

	};

	this.setupBoard = function(notation){

		startPos = notation[0].split(',');
		fSteps   = notation[1].split(',');
		bSteps   = notation[2].split(',');
		curStep  = 0;

		this.clearBoard();
		this.isNotAnimating = false;
		setupXXX(0);
	};

	this.clearBoard = function(){
		for(var i in pieces){
			var ps = pieces[i].style;
			ps.top  = '0px';
			ps.left = '0px';
			ps.visibility = 'hidden';
			ps.border = 'none';
			pieces[i].old = [0,0];
			pieces[i].popComplete = function(){};
			window.makeNote('');
		}
	};


	this.showEntireSolution = function(moveString){
	};

	var movePiece = function(v,dur){



		var x = v.split(':');
		var p = pieces[x[0]];
		var c = coords[x[1]];
		var now = +new Date;
		var diff = (now - p.oldTime)/dur;

		if(diff < 1 ){

			var per  = tForm.easeIn(diff);

	var tmpT = Math.round(p.old[0] + per*(c[0]-p.old[0])) - p.hh2;
	var tmpL = Math.round(p.old[1] + per*(c[1]-p.old[1])) - p.ww2;

			p.style.top  = tmpT +'px';
			p.style.left = tmpL +'px';
			var tmpF = function(){movePiece(v,dur);};
			setTimeout(tmpF,10);

		}else{
			CM.isNotAnimating = true;
			p.style.top  = c[0] - p.hh2 + 'px';
			p.style.left = c[1] - p.ww2 + 'px';
			p.old = coords[x[1]];
			//p.moveComplete();
		}

	};

	var curStep = 0;

	this.nextStep = function(){
		//alert('next step '+this.isNotAnimating);
		if(curStep < fSteps.length && this.isNotAnimating){
		var v = fSteps[curStep].split(':');
		if(v[1].indexOf(';') != -1){
			var tmpV = v[1].split(';');
			v[1] = tmpV[0];
			window.makeNote(tmpV[1]);
		}

		if(v[1] == '!'){//power move
			this.isNotAnimating = false;
			pieces[v[0]].oldTime = +new Date;
			pieces[v[0]].pow();
		}else if(v[1] == 'x'){//delete piece
			this.isNotAnimating = false;
			pieces[v[0]].oldTime = +new Date;
			pieces[v[0]].pin();
		}else if(v[1].indexOf('z') == 0){//add piece
			this.isNotAnimating = false;
			var tmpXY = coords[v[1].split('@')[1]];
			pieces[v[0]].old = tmpXY;
			pieces[v[0]].style.visibility = 'visible';
			pieces[v[0]].style.top  = tmpXY[0] +'px';
			pieces[v[0]].style.left = tmpXY[1] +'px';
			pieces[v[0]].popComplete = function(){
			      CM.isNotAnimating = true;
			}
			pieces[v[0]].oldTime = +new Date;
			pieces[v[0]].pop();
		}else{//move piece
			this.isNotAnimating = false;
			pieces[v[0]].oldTime = +new Date;
			movePiece(fSteps[curStep].split(';')[0],movDur);
		}
		curStep++;
	}};

	this.prevStep = function(){if(curStep > 0 && this.isNotAnimating){

		//   most 'un-do' moves have not been entered yet
		//   let's try to intercept that here
		if(!bSteps[curStep-1]){
			makeNote('backsteps unavailable...<p/><i class="Q">upgrade to version 2.0</i>');
			return;
		}

		curStep--;
		var v = bSteps[curStep].split(':');

		if(v[1].indexOf(';') != -1){
			var tmpV = v[1].split(';');
			v[1] = tmpV[0];
			window.makeNote(tmpV[1]);
		}
		if(v[1] == '!'){
			window.makeNote('');
		}else if(v[1] == 'x'){
			this.isNotAnimating = false;
			pieces[v[0]].oldTime = +new Date;
			pieces[v[0]].pin();
		}else if(v[1].indexOf('z') == 0){
			this.isNotAnimating = false;
			var tmpXY = coords[v[1].split('@')[1]];
			pieces[v[0]].old = tmpXY;
			pieces[v[0]].style.visibility = 'visible';
			pieces[v[0]].style.top  = tmpXY[0] +'px';
			pieces[v[0]].style.left = tmpXY[1] +'px';
			pieces[v[0]].popComplete = function(){
				CM.isNotAnimating = true;
			}
			pieces[v[0]].oldTime = +new Date;
			pieces[v[0]].pop();
		}else{
			this.isNotAnimating = false;
			pieces[v[0]].oldTime = +new Date;
			movePiece(bSteps[curStep].split(';')[0],movDur);
		}
	}};


};
